package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.AccountMapper;
import org.csu.mypetstore.persistence.SignonMapper;
import org.csu.mypetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

@Service("accountServiceImpl")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    //登录
    @Autowired
    private SignonMapper signonMapper;
    @Override
    public boolean login(String username, String password) {
        //查用户
        Signon signon=new Signon();
        QueryWrapper<Signon>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        signon=signonMapper.selectOne(queryWrapper);
        if ( signon.getPassword().equals(password))
        {
            return true;
        }
        return false;
    }

    @Override
    public Account getAccount(String username,HttpSession session)
    {
        Account account=new Account();
        account=accountMapper.getAccountByUsername(username);
        if (session.getAttribute(username)==null)
        {
            session.setAttribute("username",username);
        }
        return account;
    }

    @Override
    public void ModeladdAttribute(Model model,Account account)
    {
        model.addAttribute("username",account.getUsername());
        model.addAttribute("password",account.getPassword());
        model.addAttribute("address1",account.getAddress1());
        model.addAttribute("address2",account.getAddress2());
        model.addAttribute("city",account.getCity());
        model.addAttribute("state",account.getState());
        model.addAttribute("email",account.getEmail());
        model.addAttribute("zip",account.getZip());
        model.addAttribute("country",account.getCountry());
        model.addAttribute("firstname",account.getFirstName());
        String lastname=account.getLastName();
        model.addAttribute("lastname",account.getLastName());
        model.addAttribute("favoritecategory",account.getFavouriteCategoryId());
        model.addAttribute("phone",account.getPhone());
        model.addAttribute("language",account.getLanguagePreference());
    }

    @Override
    public Account getAccount(String username, String password)
    {
        return accountMapper.selectOne(username);
    }

    @Transactional
    @Override
    public void insertAccount(Account account) {
        accountMapper.insertAccount(account);
        accountMapper.insertProfile(account);
        accountMapper.insertSignon(account);
    }

    @Transactional
    @Override
    public void updateAccount(Account account) {
        accountMapper.updateAccount(account);
                accountMapper.updateProfile(account);
        if (account.getPassword() != null && !account.getPassword().isEmpty()) {
            accountMapper.updateSignon(account);
        }
    }

    @Override
    public void insertSignon(String username, String password) {
        accountMapper.insertSignon(username, password);
    }

    @Override
    public void deleteAccount(String username) {
        accountMapper.deleteAccount(username);
    }

    @Override
    public void InsertAccount(Account account){
        insertAccount(account);
    }

}
